//
//  Colormap.m
//  V3d_Cocoa
//
//  Created by Y my on 11-3-24.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Colormap.h"



@implementation ColorMap

@synthesize len, map2d, map1d;

- (id)initWithImageDisplayColorType:(ImageDisplayColorType)c {
	self = [super init];
	if (self) {
		[self _initialize:c length:256];
	}
	return self;
}

- (id)initWithImageDisplayColorType:(ImageDisplayColorType)c length:(NSUInteger)l {
	self = [super init];
	if (!self) {
		[self _initialize:c length:l];
	}
	return self;
}

- (void)_initialize:(ImageDisplayColorType)c length:(NSUInteger)length {
	// Initialize variables
	
	len = length+1;
	ctype = c;
	map1d=0; map2d=0;
	
	// Allocate memory
	
	map1d = malloc(len*3);
	if (!map1d)
	{
		printf("Fail to create colormap map1d!\n");
		len=0;
		return;
	}
	
	map2d = malloc(len);
	if (!map2d)
	{
		printf("Fail to create colormap map2d!\n");
		if (map1d) {free(map1d); map1d=0;}
		len=0;
		return;
	}
	
	int i;
	for (i=0;i<len;i++)
	{
		map2d[i] = map1d+(V3DLONG)(i)*3;
	}
	
	// Set the colormap values
	
	unsigned char simple_cmap[14*3]  = {0,   0,   0,
		255, 255, 255, 
		255, 0,   0,
		0,   255, 0,
		0,   0,   255,
		255, 255, 0,
		0,   255, 255,
		255, 0,   255,
		255, 128,   0,
		128, 255, 0,
		0,   128, 255,
		255, 255, 128,
		128, 255, 255,
		255, 128, 255
	};
	
	//add arnim_cmap on 070717						   
	unsigned char arnim_cmap[52*3] = {  0, 0, 0, 
		254, 0, 0, 
		254, 0, 0, 
		254, 140, 0, 
		254, 140, 0, 
		254, 204, 0, 
		254, 204, 0, 
		102, 25, 0, 
		102, 25, 0, 
		0, 157, 0, 
		152, 254, 0, 
		28, 254, 10, 
		0, 95, 0, 
		40, 115, 203, 
		40, 115, 203, 
		162, 0, 208, 
		162, 0, 208, 
		41, 203, 103, 
		41, 203, 103, 
		170, 203, 41, 
		170, 203, 41, 
		203, 40, 116, 
		203, 40, 116, 
		50, 40, 203, 
		50, 40, 203, 
		203, 40, 48, 
		203, 40, 48, 
		170, 203, 41, 
		170, 203, 41, 
		203, 70, 41, 
		203, 70, 41, 
		0, 102, 254, 
		0, 102, 254, 
		232, 46, 227, 
		232, 46, 227, 
		50, 40, 203, 
		50, 40, 203, 
		241, 215, 23, 
		241, 215, 23, 
		236, 134, 47, 
		236, 134, 47, 
		134, 203, 41, 
		25, 218, 155, 
		25, 218, 155, 
		50, 40, 203, 
		50, 40, 203, 
		203, 40, 40, 
		203, 40, 40, 
		161, 116, 250, 
		161, 116, 250, 
		51, 52, 254, 
		41, 41, 203
	};
	
	//080810
	unsigned char hanchuan_cmap[63*3] = {  
		0,0,0, // 179, 204, 204, //display the first cyan background as black
		255, 0, 0,
		255, 205, 0,
		255, 141, 0,
		102, 25, 0,
		28, 255, 10,
		0, 158, 0,
		40, 41, 204,
		153, 255, 0,
		36, 102, 20,
		50, 40, 204,
		204, 40, 108,
		135, 204, 40,
		40, 66, 204,
		255, 204, 49,
		67, 0, 179,
		40, 204, 115,
		71, 178, 122,
		0, 102, 255,
		204, 40, 149,
		51, 102, 255,
		230, 123, 26,
		0, 95, 0,
		165, 37, 195,
		204, 135, 23,
		255, 226, 3,
		95, 40, 204,
		111, 197, 62,
		50, 237, 255,
		190, 0, 0,
		163, 101, 0,
		72, 204, 40,
		42, 123, 176,
		160, 150, 255,
		255, 0, 0,
		255, 205, 0,
		255, 141, 0,
		102, 25, 0,
		40, 41, 204,
		24, 126, 32,
		36, 102, 20,
		50, 40, 204,
		204, 40, 108,
		40, 66, 204,
		255, 204, 49,
		67, 0, 179,
		40, 204, 115,
		71, 178, 122,
		0, 102, 255,
		204, 40, 149,
		51, 102, 255,
		230, 123, 26,
		165, 37, 195,
		204, 135, 23,
		255, 226, 3,
		95, 40, 204,
		111, 197, 62,
		50, 237, 255,
		190, 0, 0,
		163, 101, 0,
		72, 204, 40,
		42, 123, 176,
		160, 150, 255
	};
	
	
	unsigned char *amap=0;
	V3DLONG colorMapSz;
	register int t;
	
	switch (ctype)
	{
		case colorArnimFlyBrainColor:
			amap = arnim_cmap;
			colorMapSz = sizeof(arnim_cmap)/3; 
			
			if (len<1)
				break;
			
			t=0;
			
			map2d[0][0] = amap[t*3+0];
			map2d[0][1] = amap[t*3+1];
			map2d[0][2] = amap[t*3+2];
			
			for (i=1;i<len;i++)
			{
				t=(i-1)%(colorMapSz-1)+1; //070717
				
				map2d[i][0] = amap[t*3+0];
				map2d[i][1] = amap[t*3+1];
				map2d[i][2] = amap[t*3+2];
				
			}
			break;
			
		case colorHanchuanFlyBrainColor:
			amap = hanchuan_cmap;
			colorMapSz = sizeof(hanchuan_cmap)/3; 
			
			if (len<1)
				break;
			
			t=0;
			
			map2d[0][0] = amap[t*3+0];
			map2d[0][1] = amap[t*3+1];
			map2d[0][2] = amap[t*3+2];
			
			for (i=1;i<len;i++)
			{
				t=(i-1)%(colorMapSz-1)+1; 
				map2d[i][0] = amap[t*3+0];
				map2d[i][1] = amap[t*3+1];
				map2d[i][2] = amap[t*3+2];
				
			}
			break;
			
		case colorPseudoMaskColor:
		default:
			amap = simple_cmap;
			colorMapSz = sizeof(simple_cmap)/3; 
			
			if (len<1)
				break;
			
			t=0;
			
			map2d[0][0] = amap[t*3+0];
			map2d[0][1] = amap[t*3+1];
			map2d[0][2] = amap[t*3+2];
			
			for (i=1;i<len;i++)
			{
				t=(i-1)%(colorMapSz-1)+1; //070717
				
				map2d[i][0] = amap[t*3+0];
				map2d[i][1] = amap[t*3+1];
				map2d[i][2] = amap[t*3+2];
				
			}
			break;
	}
}
@end
